/*
 * PlaylistInsertAction.java
 *
 * Created on April 8, 2006, 1:28 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package com.bff.slimserver.musicjukebox.lib.actions;

import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import com.bff.slimserver.musicjukebox.ui.components.savedplaylist.SavedPlaylistDialog;
import com.bff.bjj.utils.Resources;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import org.bff.slimserver.SlimSavedPlaylistManager;
import org.bff.slimserver.exception.SlimConnectionException;
import org.bff.slimserver.exception.SlimException;
import org.bff.slimserver.musicobjects.SlimSong;

/**
 *
 * @author Bill
 */
public class AddToSavedPlaylistAction extends AbstractAction {

    private JFrame parent;
    private List<SlimSong> items;
    private SlimSavedPlaylistManager playlistManager;

    /** Creates a new instance of PlaylistInsertAction */
    public AddToSavedPlaylistAction(JFrame parent, SlimSong item) {
        this(parent, new ArrayList<SlimSong>());
        this.items.add(item);
    }

    public AddToSavedPlaylistAction(JFrame parent, Collection<SlimSong> items) {
        this.items = new ArrayList<SlimSong>(items);
        this.parent = parent;
        try {
            this.playlistManager = JukeboxProperties.getInstance().getPlaylistManager();
        } catch (SlimConnectionException ex) {
            Logger.getLogger(AddToSavedPlaylistAction.class.getName()).log(Level.SEVERE, null, ex);
        }

        putValue(Action.NAME, Resources.getMessage(Resources.LABEL_KEY.ACTION_PLAYLIST_ADD_NAME));
        putValue(Action.SHORT_DESCRIPTION, Resources.getMessage(Resources.LABEL_KEY.ACTION_PLAYLIST_ADD_SHORT_DESC));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        SavedPlaylistDialog dlg = new SavedPlaylistDialog(parent);
        dlg.setVisible(true);

        if (dlg.getSelectedPlaylist() != null) {
            try {
                this.playlistManager.addPlaylistSongs(dlg.getSelectedPlaylist(), this.items);
            } catch (SlimException ex) {
                Logger.getLogger(AddToSavedPlaylistAction.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
